<template>
  <Card class="modify_pwd">
  <div>
      <Form ref="formValidate" :model="formValidate" :rules="ruleValidate"  label-position="left" :label-width="80">
      <Form-item label="旧密码" prop="oldpwd">
          <Input v-model="formValidate.oldpwd" ></Input>
      </Form-item>
      <Form-item label="新密码" prop="newpwd">
          <Input id="newpwd" v-model="formValidate.newpwd" class='password-number' detect-change="off" initial="off" placeholder="请输入新密码" type="password"></Input>
          <span >只能输入6-18个字母、数字、下划线</span>
      </Form-item>
      <Form-item label="确认密码" prop="newpwd2">
          <Input v-model="formValidate.newpwd2" class='password-number' initial="off" placeholder="请输入确认密码" type="password"></Input>
      </Form-item>
      <div class="add-group">
        <Button type="primary" @click="onSubmit">提交</Button>
      </div>
      </Form>
  </div>
  </Card>
</template>
<script>
// import VueMarkdown from 'vue-markdown'
import {modifyPwd} from '@/api/user.js'
import {validatPasswad} from '@/common/validate.js'
import {setToken, setUserPwd} from '@/common/auth'
export default {
  data () {
    return {
      ruleValidate: {
        oldpwd: [{required: true, message: '旧密码不能为空', trigger: 'blur'}],
        newpwd: [{required: true, message: '新密码不能为空', trigger: 'blur'}],
        newpwd2: [{required: true, message: '确认密码不能为空', trigger: 'blur'}]
      },
      formValidate: {
        oldpwd: '',
        newpwd: '',
        newpwd2: ''
      }
    }
  },
  created () {
    // console.log('created')
  },
  methods: {
    onSubmit () {
      let oldpwd = this.formValidate.oldpwd
      let newpwd = this.formValidate.newpwd
      let newpwd2 = this.formValidate.newpwd2
      if (oldpwd === newpwd) {
        this.$Message.error('新密码和旧密码一致!!!')
        return
      }

      if (newpwd2 !== newpwd) {
        this.$Message.error('新密码和确认密码不一致!!!')
        return
      }

      if (!validatPasswad(newpwd)) {
        this.$Message.error('只能输入6-18个字母、数字、下划线!!!')
        return
      }

      modifyPwd(oldpwd, newpwd).then((data) => {
        if (!data) {
          return
        }
        console.log('modifyPwd data：', data)
        let success = data.data.data.intValues[0]
        let token = data.data.data.strValues[0]
        if (success === 1) {
          setToken(token)
          setUserPwd(newpwd)
          window.location.href = '/slotmanager'
        } else {
          this.$Message.error('修改密码失败!!!')
        }
      })
    }
  },
  computed: {
    // console.log('computed')
  }
}
</script>
<style scoped>
  .modify_pwd {
    width: 500px;
    margin: 60px auto;
  }
  .response {
      font-size:14px;
      color: #3e3e3e;
  }
</style>
